//
//  NYTagViewController.m
//  BS
//
//  Created by neal on 15/8/16.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYTagViewController.h"
#import "NYTagCell.h"
#import "NYTagModel.h"


@interface NYTagViewController ()
@property(nonatomic ,weak) AFHTTPSessionManager *manager;
/* 模型 */
@property (nonatomic,strong)  NSArray *tagModel;

@end

@implementation NYTagViewController
- (AFHTTPSessionManager *)manager
{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}

static NSString * const ID = @"tag";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"标签订阅";
    [self setuptableView];
    [SVProgressHUD showWithStatus:@"正在拼命加载数据..."];
    [self setupLoadData];
    
}
- (void)setupLoadData
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"]= @"tag_recommend";
    parameters[@"action"] = @"sub";
    parameters[@"c"] = @"topic";
    NYWeakSelf;
    [self.manager GET:NYBaseURL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {

        [SVProgressHUD dismiss];
        
        weakSelf.tagModel = [NYTagModel objectArrayWithKeyValuesArray:responseObject];
        [weakSelf.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络不接力~"];
    }];

}
- (void)setuptableView
{
    self.tableView.rowHeight = 70;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.tableView.backgroundColor = NYGlobalBg;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NYTagCell class]) bundle:nil] forCellReuseIdentifier:ID];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [SVProgressHUD dismiss];
    //取消请求的两种方式
   // [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
    [self.manager invalidateSessionCancelingTasks:YES];//直接弄死任务，取消所有任务
    
}
#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tagModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NYTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.tagModel = self.tagModel[indexPath.row];
    return cell;
}

@end
